﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class YGRABDOC
    { 
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                DataRow dwData = SetData.ICC_QUERYTB.NewRow();
                dwData["ABPAUFKRS"] = CKRS;
                dwData["ABPAUFLFD"] = CLFD;
                dwData["ABPART"] = CART;
                dwData["ABPFARBE"] = CFARBE;
                DataSet SResult = INTEXDBC.QueryABPOS(dwData, 31);
                SetData.Clear();
                SetData.Merge(SResult);
                vwXABPOS.View.MoveCurrentToFirst();
                SetDetail.Clear();                
                btnCombine_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        //MainViewModel MainViewModel;
        string CKRS = "", CLFD = "", CPOS = "", CART = "", CFARBE = "";
        double QTYBASE = 0;
        double QTYTOTAL = 0;
        DataRow dwBase = null;
        private void btnGotoCombine_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwABPOS == null)
                {
                    MessageBox.Show("请选择生产单行");
                    return;
                }
                tabCombine.Visibility = Visibility.Visible;
                CKRS = dwABPOS["ABPAUFKRS"].ToString();
                CLFD = dwABPOS["ABPAUFLFD"].ToString();
                CPOS = dwABPOS["ABPAUFPOS"].ToString();
                CART = dwABPOS["ABPART"].ToString();
                CFARBE = dwABPOS["ABPFARBE"].ToString();
                dwBase = null;
                QTYTOTAL = 0;
                QTYBASE = Convert.ToDouble(dwABPOS["ABPSOLLMENGE5"].ToString());
                tabCombine.Focus();
                btnRefresh_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void tabCombine_LostFocus(object sender, RoutedEventArgs e)
        {
            tabCombine.Visibility = Visibility.Collapsed;
        }

        private void btnCombine_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                if (CLFD == "")
                {
                    MessageBox.Show("请先选择要合并的生产单");
                    return;
                }
                if (dwXABPOS == null)
                {
                    MessageBox.Show("请先选择要合并的生产单");
                    return;
                }
                string _KRS = dwXABPOS["ABPAUFKRS"].ToString();
                string _LFD = dwXABPOS["ABPAUFLFD"].ToString();
                string _POS =  dwXABPOS["ABPAUFPOS"].ToString();
                vwXABPOS.View.MoveCurrentTo(-1);
                vwTABPOS.View.MoveCurrentTo(-1);
                if (dwBase == null)
                {
                    DataRow[] rows = SetData.Tables["ABPOS"].Select("ABPAUFKRS='" + CKRS + "' AND ABPAUFLFD='" + CLFD + "' AND ABPAUFPOS='" + CPOS + "'");
                    foreach (DataRow row in rows)
                    {
                        DataRow dwData = SetDetail.Tables["ABPOS"].Rows.Add();
                        dwData.ItemArray = row.ItemArray;
                        dwBase = dwData;
                        row.Delete();
                    }
                }                 
                else
                {
                    DataRow[] rows = SetData.Tables["ABPOS"].Select("ABPAUFKRS='" + _KRS + "' AND ABPAUFLFD='" + _LFD + "' AND ABPAUFPOS='" + _POS + "'");
                    foreach (DataRow row in rows)
                    {
                        DataRow dwData = SetDetail.Tables["ABPOS"].Rows.Add();
                        dwData.ItemArray = row.ItemArray; 
                        row.Delete();
                    }
                }
                SetData.AcceptChanges();
                SetDetail.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnCombineConvert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string msg = "";
                string filter = "";
                DataRow[] rows = null;
                //建立需要合并的生产单列表              
                //放大母生产单数量
                //删除其他生产单
                //找到这些生产单对应的AB52，修改生产单号码和行号，进行行号更新，保存
                //找到ICC_ABAX,修改行号，进行行号更新
                if (SetDetail.ABPOS.Rows.Count < 2)
                {
                    MessageBox.Show("再来一个!");
                    return;
                }
                if (dwBase == null)
                    return;
                EventDispatcher.Instance.RaiseStatus("开始合投", StatusEventType.RunningProgress);
                #region======== //找到这些生产单对应的AB52，修改生产单号码和行号，进行行号更新，保存
                List<DataRow> ListRow = new List<DataRow>();
                DataSet STemp = new DataSet();
                DataSet SReult = new DataSet();
                DataRow dwData = SetData.ICC_QUERYTB.NewRow();
                if (SetDetail.Tables.Contains("ABAUFM"))
                {
                    SetDetail.Tables["ABAUFM"].Clear();
                }
                if (SetDetail.Tables.Contains("ICC_ABAX"))
                {
                    SetDetail.Tables["ICC_ABAX"].Clear();
                }
                if (SetDetail.Tables.Contains("GAAUF"))
                {
                    SetDetail.Tables["GAAUF"].Clear();
                }
                double VKPTOTAL = 0;
                foreach (DataRow row in SetDetail.ABPOS.Rows)
                {
                    QTYTOTAL += Convert.ToDouble(row["ABPSOLLMENGE5"].ToString());
                    VKPTOTAL += Convert.ToDouble(row["ABPSOLLMENGE7"].ToString());
                    dwData["ABAMANDANT"] = row["ABPMANDANT"];
                    dwData["ABAAUFKRS"] = row["ABPAUFKRS"];
                    dwData["ABAAUFLFD"] = row["ABPAUFLFD"];
                    dwData["ABAAUFPOS"] = row["ABPAUFPOS"];
                    SReult = INTEXDBC.QueryABAUFM(dwData, 5);
                    STemp.Merge(SReult);
                    SReult = INTEXDBC.QueryICC_ABAX(dwData, 5);
                    STemp.Merge(SReult);
                    dwData["GAMANDANT"] = row["ABPMANDANT"];
                    dwData["GAAUFKRS"] = row["ABPAUFKRS"];
                    dwData["GAAUFLFD"] = row["ABPAUFLFD"];
                    dwData["GAAUFPOS"] = row["ABPAUFPOS"];
                    SReult = INTEXDBC.QueryGAAUF(dwData, 16);
                    STemp.Merge(SReult);
                }
                SetDetail.Merge(STemp);
                #region============== 放大母生产单数量===========
                filter = "GAKG6>0";
                rows = SetDetail.Tables["GAAUF"].Select(filter);
                if (rows.Length > 0)
                {
                    MessageBox.Show("已经有了作业记录，不允许合并");
                    return;
                }
                dwBase["ABPSOLLMENGE5"] = QTYTOTAL;
                dwBase["ABPSOLLMENGE7"] = VKPTOTAL;
      
                double RATIO = Math.Round(QTYTOTAL / QTYBASE, 5);
                msg = "合并后的数量是:" + QTYTOTAL + ",订单量" + VKPTOTAL + ",投料比:" + RATIO;
                if (MessageBox.Show(msg+"!","请确认",MessageBoxButton.YesNoCancel)!= MessageBoxResult.Yes)
                {
                    return;
                }                
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.RunningProgress);
                msg = "更新生产单:" + dwBase["ABPAUFKRS"].ToString() + dwBase["ABPAUFLFD"].ToString() + dwBase["ABPAUFLFD"].ToString() + ",ABPSOLLMENGE5:" + dwBase["ABPSOLLMENGE5"].ToString() + ",ABPSOLLMENGE7:" + dwBase["ABPSOLLMENGE7"].ToString();
                EventDispatcher.Instance.RaiseStatus(msg+"开始", StatusEventType.RunningProgress);
                INTEXDBC.UpdateABPOS(dwBase, 7);
                INTEXDBC.UpdateABFOLGE(dwBase, 4);
                EventDispatcher.Instance.RaiseStatus(msg+"完成", StatusEventType.RunningProgress);
                filter = "GAAUFKRS='" + CKRS + "' AND GAAUFLFD='" + CLFD + "' AND GAAUFPOS='" + CPOS + "'";
                rows = SetDetail.Tables["GAAUF"].Select(filter);
                foreach (DataRow row in rows)
                {
                    row["GAKG1"] = Math.Round(Convert.ToDouble(row["GAKG1"].ToString()) * RATIO);
                    row["GAKG2"] = Math.Round(Convert.ToDouble(row["GAKG2"].ToString()) * RATIO);
                    INTEXDBC.UpdateGAAUF(row, 7);
                }
                foreach (DataRow row in SetDetail.Tables["GAAUF"].Rows)
                {
                    m_Data.GD.RemoveAllocation(row);
                }
                INTEXDBC.UpdateABPOS(dwBase, 6);
                msg = "更新GAAUF:" + dwBase["ABPAUFKRS"].ToString() + dwBase["ABPAUFLFD"].ToString() + dwBase["ABPAUFLFD"].ToString();             
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.RunningProgress);
                #endregion
                filter = "ABAAUFKRS='" + CKRS + "' AND ABAAUFLFD='" + CLFD + "' AND ABAAUFPOS='" + CPOS + "' AND ABAAPOS='0001'";
                rows = SetDetail.Tables["ABAUFM"].Select(filter);
                DataRow dwMUBase = null;
                foreach (DataRow row in rows)
                {
                    dwMUBase = row;
                }
                if (dwMUBase == null)
                    return;
                EventDispatcher.Instance.RaiseStatus("处理留位关系", StatusEventType.RunningProgress);
                int ABALEAPOS = Convert.ToInt16(dwMUBase["ABALEAPOS"].ToString());
                foreach (DataRow row in SetDetail.Tables["ABAUFM"].Rows)
                {
                    string VKAUFKRS = row["ABAVKAUFKRS"].ToString();
                    string VKAUFLFD = row["ABAVKAUFLFD"].ToString();
                    string VKAUFPOS = row["ABAVKAUFPOS"].ToString();
                    string AUFKRS = row["ABAAUFKRS"].ToString();
                    string AUFLFD = row["ABAAUFLFD"].ToString();
                    string AUFPOS = row["ABAAUFPOS"].ToString();
                    string ABAAPOS = row["ABAAPOS"].ToString();
                    if (VKAUFLFD.ToString() == "000000")
                        continue;
                    if (AUFKRS + AUFLFD + AUFPOS == CKRS + CLFD + CPOS)
                        continue;
                    ABALEAPOS++;
                    row["ABAAUFKRS"] = CKRS;
                    row["ABAAUFLFD"] = CLFD;
                    row["ABAAUFPOS"] = CPOS;
                    row["ABAAPOS"] = Helpers.Helper.Right("0000" + ABALEAPOS.ToString(), 4);
                    ListRow.Add(row);
                    var query = from DataRow dwABAX in SetDetail.Tables["ICC_ABAX"].AsEnumerable()
                                where dwABAX["ABAAUFKRS"].ToString() == AUFKRS
                                && dwABAX["ABAAUFLFD"].ToString() == AUFLFD
                                && dwABAX["ABAAUFPOS"].ToString() == AUFPOS
                                && dwABAX["ABAVKAUFKRS"].ToString() == VKAUFKRS
                                  && dwABAX["ABAVKAUFLFD"].ToString() == VKAUFLFD
                                    && dwABAX["ABAVKAUFPOS"].ToString() == VKAUFPOS
                                select dwABAX;
                    foreach (DataRow dwABAX in query)
                    {
                        dwABAX["ABAAUFKRS"] = CKRS;
                        dwABAX["ABAAUFLFD"] = CLFD;
                        dwABAX["ABAAUFPOS"] = CPOS;
                        dwABAX["ABAAPOS"] = row["ABAAPOS"];
                        ListRow.Add(dwABAX);
                    }
                }

                dwMUBase["ABASGESGEW"] = QTYTOTAL*10;
                dwMUBase["ABALEAPOS"] = ABALEAPOS;
                INTEXDBC.UpdateABAUFM(dwMUBase, 7);

                m_Data.ABP.SaveAB(app.company, ListRow);
                #endregion

                #region======== //删除其他生产单
                EventDispatcher.Instance.RaiseStatus("删除其他生产单", StatusEventType.RunningProgress);
                foreach (DataRow row in SetDetail.ABPOS.Rows)
                {
                    string KKRS = row["ABPAUFKRS"].ToString();
                    string KLFD = row["ABPAUFLFD"].ToString();
                    string KPOS = row["ABPAUFPOS"].ToString();
                    if (KKRS + KLFD + KPOS == CKRS + CLFD + CPOS)
                        continue;
                    m_Data.ABP.DELTEABPOS(app.Mandant, KKRS, KLFD, KPOS);
                }
                #endregion
                SetDetail.Clear();
                CLFD = "";
                SetData.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("合投有异常发生"+ex.Message);
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("异常", StatusEventType.StopProgress);
            }
            finally
            {
                dwBase = null;
                EventDispatcher.Instance.RaiseStatus("合投完成", StatusEventType.StopProgress);
            }
        }
        private void btnRemoveFromCombine_Click(object sender, RoutedEventArgs e)
        {
            if (dwTABPOS == null)
                return;
            DataRow[] rows = SetDetail.Tables["ABPOS"].Select("ABPAUFKRS='" + dwTABPOS["ABPAUFKRS"].ToString() + "' AND ABPAUFLFD='" + dwTABPOS["ABPAUFLFD"].ToString() + "' AND ABPAUFPOS='" + dwTABPOS["ABPAUFPOS"].ToString() + "'");
            foreach (DataRow row in rows)
            {
                DataRow dwData = SetData.Tables["ABPOS"].Rows.Add();
                dwData.ItemArray = row.ItemArray;
                row.Delete();
            }
            SetDetail.AcceptChanges();
        }
        private void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            if (txtFlagSplit.Text.ToUpper() != "Y")
            {
                MessageBox.Show("请手工将拆分标志设置为Y");
                return;
            }
            if (txtSplitQty.Text.ToUpper() == "-1")
            {
                MessageBox.Show("请输入有效的拆分数量");
                return;
            }
            if (txtPOS1.Text.ToUpper() == "000" || txtPOS1.Text.ToUpper() == "")
            {
                MessageBox.Show("请输入有效的批次，例如011,注意不能和现有的批次重复");
                return;
            }
            if (dwTABPOS == null)
            {
                MessageBox.Show("请选择对应的粗纱生产单");
                return;
            }
            if (dwTABPOS["ABPPOSTYP"].ToString().Trim() != "HM")
            {
                MessageBox.Show("仅对混棉单支持分批");
                return;
            }
            try
            {
                string filter = "";
                DataRow[] rows = null;
                DataTable tb = null;
                double SPLITQTY = Convert.ToDouble(txtSplitQty.Text);
                double QTYTOTAL = 0;
                //定位需要处理的生产单              
                //拆分数量
                //更新，并创建

                if (SetDetail.ABPOS.Rows.Count == 0)
                    return;
                EventDispatcher.Instance.RaiseStatus("开始合投", StatusEventType.RunningProgress);
                #region======== //找到这些生产单对应的AB52，修改生产单号码和行号，进行行号更新，保存
                //哪个汉字不懂？找到这些生产单对应的AB52，修改生产单号码和行号，进行行号更新，保存
                QTYTOTAL = Convert.ToDouble(dwTABPOS["ABPSOLLMENGE7"].ToString()) / 100;
                List<DataRow> ListRow = new List<DataRow>();
                DataSet STemp = new DataSet();
                DataSet SReult = new DataSet();
                DataRow dwData = SetData.ICC_QUERYTB.NewRow();
                string Client = dwTABPOS["ABPMANDANT"].ToString();
                KRS = dwTABPOS["ABPAUFKRS"].ToString();
                LFD = dwTABPOS["ABPAUFLFD"].ToString();
                POS = dwTABPOS["ABPAUFPOS"].ToString();
                if (POS != "010")
                {
                    //MessageBox.Show("仅对010支持分批");
                    //return;
                }
                dwData = SetData.ICC_QUERYTB.NewRow();
                if (SetDetail.Tables.Contains("ABPOS")) 
                {
                    SetDetail.Tables["ABPOS"].Clear();
                    dwData["ABPMANDANT"] = Client;
                    dwData["ABPAUFKRS"] = KRS;
                    dwData["ABPAUFLFD"] = LFD;
                    dwData["ABPAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryABPOS(dwData, 1);
                    SetDetail.Merge(SReult);
                }
                if (SetDetail.Tables.Contains("ABAUFM")) 
                {
                    SetDetail.Tables["ABAUFM"].Clear();
                    dwData["ABAMANDANT"] = Client;
                    dwData["ABAAUFKRS"] = KRS;
                    dwData["ABAAUFLFD"] = LFD;
                    dwData["ABAAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryABAUFM(dwData, 5);
                    SetDetail.Merge(SReult);
                }
                if (SetDetail.Tables.Contains("ICC_ABAX")) 
                {
                    SetDetail.Tables["ICC_ABAX"].Clear();
                    dwData["ABAMANDANT"] = Client;
                    dwData["ABAAUFKRS"] = KRS;
                    dwData["ABAAUFLFD"] = LFD;
                    dwData["ABAAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryICC_ABAX(dwData, 5);
                    SetDetail.Merge(SReult);
                    dwData["ABAVKAUFKRS"] = KRS;
                    dwData["ABAVKAUFLFD"] = LFD;
                    dwData["ABAVKAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryICC_ABAX(dwData, 4);
                    SetDetail.Merge(SReult);
                }
                if (SetDetail.Tables.Contains("GAAUF"))
                {
                    SetDetail.Tables["GAAUF"].Clear();
                    dwData["GAMANDANT"] = Client;
                    dwData["GAAUFKRS"] = KRS;
                    dwData["GAAUFLFD"] = LFD;
                    dwData["GAAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryGAAUF(dwData, 2);
                    SetDetail.Merge(SReult);
                }
                if (SetDetail.Tables.Contains("ABFOLGE"))
                {
                    SetDetail.Tables["ABFOLGE"].Clear();
                    dwData["ABFMANDANT"] = Client;
                    dwData["ABFAUFKRS"] = KRS;
                    dwData["ABFAUFLFD"] = LFD;
                    dwData["ABFAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryABFOLGE(dwData, 1);
                    SetDetail.Merge(SReult);
                }
                if (SetDetail.Tables.Contains("ABPR"))
                {
                    SetDetail.Tables["ABPR"].Clear();
                    dwData["ARMANDANT"] = Client;
                    dwData["ARAUFKRS"] = KRS;
                    dwData["ARAUFLFD"] = LFD;
                    dwData["ARAUFPOS"] = POS;
                    SReult = INTEXDBC.QueryABPR(dwData, 1);
                    SetDetail.Merge(SReult);
                }
                #endregion
                #region==============修改生产单数量===========
                string POS1 = Helpers.Helper.Right("000" + txtPOS1.Text, 3);
                double RATIO = Math.Round(SPLITQTY / QTYTOTAL, 5);
                DataRow dwTemp = null;
                foreach (DataRow row in SetDetail.Tables["ABPOS"].Rows)
                {

                    double ABPSOLLMENGE5 = Convert.ToDouble(row["ABPSOLLMENGE5"].ToString());
                    double ABPSOLLMENGE7 = Convert.ToDouble(row["ABPSOLLMENGE7"].ToString());
                    row["ABPSOLLMENGE5"] = Math.Round(ABPSOLLMENGE5 * (1 - RATIO) / 100) * 100;
                    row["ABPSOLLMENGE7"] = Math.Round(ABPSOLLMENGE7 * (1 - RATIO) / 100) * 100;
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["ABPOS"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ABPAUFPOS"] = POS1;
                    dwData["ABPSOLLMENGE5"] = Math.Round(ABPSOLLMENGE5 * (RATIO) / 100) * 100;
                    dwData["ABPSOLLMENGE7"] = Math.Round(ABPSOLLMENGE7 * (RATIO) / 100) * 100;
                    ListRow.Add(dwData);
                }
                foreach (DataRow row in SetDetail.Tables["ABPR"].Rows)
                {
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["ABPR"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ARAUFPOS"] = POS1;
                    ListRow.Add(dwData);
                }

                foreach (DataRow row in SetDetail.Tables["GAAUF"].Rows)
                {
                    m_Data.GD.RemoveAllocation(row);

                    double GAKG1 = Convert.ToDouble(row["GAKG1"].ToString());
                    double GAKG2 = Convert.ToDouble(row["GAKG2"].ToString());
                    row["GAKG1"] = Math.Round(GAKG1 * (1 - RATIO) / 100) * 100;
                    row["GAKG2"] = Math.Round(GAKG2 * (1 - RATIO) / 100) * 100;
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["GAAUF"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["GAAUFPOS"] = POS1;
                    dwData["GAKG1"] = Math.Round(GAKG1 * (RATIO) / 100) * 100;
                    dwData["GAKG2"] = Math.Round(GAKG2 * (RATIO) / 100) * 100;
                    ListRow.Add(dwData);
                }

                foreach (DataRow row in SetDetail.Tables["ABFOLGE"].Rows)
                {
                    double ABFSOLLMG = Convert.ToDouble(row["ABFSOLLMG"].ToString());
                    row["ABFSOLLMG"] = Math.Round(ABFSOLLMG * (1 - RATIO) / 100) * 100;
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["ABFOLGE"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ABFAUFPOS"] = POS1;
                    dwData["ABFSOLLMG"] = Math.Round(ABFSOLLMG * (RATIO) / 100) * 100;
                    ListRow.Add(dwData);
                }
                //ABAUFM保存订单之间的数量关系
                foreach (DataRow row in SetDetail.Tables["ABAUFM"].Rows)
                {
                    double ABARESMG = Convert.ToDouble(row["ABARESMG"].ToString());
                    double ABASANZSTK = Convert.ToDouble(row["ABASANZSTK"].ToString());
                    double ABASGESGEW = Convert.ToDouble(row["ABASGESGEW"].ToString());

                    row["ABARESMG"] = Math.Round(ABARESMG * (1 - RATIO) / 100) * 100;
                    row["ABASANZSTK"] = Math.Round(ABASANZSTK * (1 - RATIO) / 100) * 100;
                    row["ABASGESGEW"] = Math.Round(ABASGESGEW * (1 - RATIO) / 100) * 100;//预留数量
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["ABAUFM"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ABAAUFPOS"] = POS1;
                    dwData["ABARESMG"] = Math.Round(ABARESMG * (RATIO) / 100) * 100;
                    dwData["ABASANZSTK"] = Math.Round(ABASANZSTK * (RATIO) / 100) * 100;
                    dwData["ABASGESGEW"] = Math.Round(ABASGESGEW * (RATIO) / 100) * 100;
                    ListRow.Add(dwData);
                }
                //ICC_ABAX保存订单之间的数量关系
                foreach (DataRow row in SetDetail.Tables["ICC_ABAX"].Rows)
                {
                    double ABARESMG = Convert.ToDouble(row["ABARESMG"].ToString());
                    double ABAQTYRESERVE = Convert.ToDouble(row["ABAQTYRESERVE"].ToString());

                    double ABASANZSTK = Convert.ToDouble(row["ABASANZSTK"].ToString());
                    row["ABARESMG"] = Math.Round(ABARESMG * (1 - RATIO) / 100) * 100;
                    row["ABAQTYRESERVE"] = Math.Round(ABAQTYRESERVE * (1 - RATIO) / 100) * 100;//预留数量
                    row["ABASANZSTK"] = Math.Round(ABASANZSTK * (1 - RATIO));
                    ListRow.Add(row);
                    dwData = SetDetail.Tables["ICC_ABAX"].NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ABAID"] = 0;
                    if (dwData["ABAVKAUFPOS"].ToString() == POS)
                    {
                        dwData["ABAVKAUFPOS"] = POS1;
                    }
                    if (dwData["ABAAUFPOS"].ToString() == POS)
                    {
                        dwData["ABAAUFPOS"] = POS1;
                    }
                    dwData["ABARESMG"] = Math.Round(ABARESMG * (RATIO) / 100) * 100;
                    dwData["ABASANZSTK"] = Math.Round(ABASANZSTK * (RATIO));
                    dwData["ABAQTYRESERVE"] = Math.Round(ABAQTYRESERVE * (RATIO) / 100) * 100;
                    ListRow.Add(dwData);
                }
                EventDispatcher.Instance.RaiseStatus("修改生产单数量", StatusEventType.RunningProgress);
                #endregion
                EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.RunningProgress);
                m_Data.ABP.SaveAB(app.company, ListRow);//保存拆分的记录
                INTEXDBC.UpdateABPOS(dwABPOS, 6);//更新生产单
                EventDispatcher.Instance.RaiseStatus("完成更新", StatusEventType.RunningProgress);
                SetDetail.Clear();
                CLFD = "";
                SetData.Clear();
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("异常退出", StatusEventType.StopProgress);
            }
            finally
            {
                txtFlagSplit.Text = "N";
                this.txtSplitQty.Text = "-1";
                this.txtPOS1.Text = "000";
            }
        }

    }
}
